
package com.putao.album.main;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.putao.album.R;
import com.putao.album.base.BaseFragment;
import com.putao.album.collection.ActivityCollection;
import com.putao.album.dialog.DialogBuilderFactory;
import com.putao.album.dialog.ShareAppDialogBuilder;
import com.putao.album.eventbus.BasePostEvent;
import com.putao.album.main.bean.RequestSideMoreInfo;
import com.putao.album.main.view.BabyHeaderIconCollectionView;
import com.putao.album.message.ActivityMessage;
import com.putao.album.popupwindow.PtAddChildPopupWindow;
import com.putao.album.popupwindow.PtInvitePopupWindow;
import com.putao.album.setting.ActivitySettings;
import com.putao.album.share.ShareTools;
import com.putao.album.tag.ActivityTagAlbum;
import com.putao.album.util.ActivityHelper;
import com.putao.album.util.CommonUtils;
import com.putao.album.util.HttpRequestUtil;
import com.putao.album.util.PuTaoConstants;

import java.util.HashMap;

import de.greenrobot.event.EventBus;

/**
 * Created by yanglun on 15/4/21.
 */
public class LeftFragment extends BaseFragment implements View.OnClickListener, PtInvitePopupWindow.PopupClickListener {
    private LinearLayout body_ll;
    private LinearLayout home_page_left_menu_collection_ll;
    private LinearLayout home_page_left_menu_tag_ll, share_to_web;
    private TextView left_menu_setting_tv, left_menu_news_tv;
    private BabyHeaderIconCollectionView baby_header_icon_col_view;
    private TextView home_page_left_menu_tag_num_tv, home_page_left_menu_collection_num_tv;
    private PopupWindow popupwindow;
    private RequestSideMoreInfo mRequestSideMoreInfo;
    String inviteRelationShip = "爸爸";
    private ShareTools mShareTools;

    @Override
    public int doGetContentViewId() {
        return R.layout.layout_album_home_left;
    }

    @Override
    public void doInitSubViews(View view) {
        super.doInitSubViews(view);
        share_to_web = queryViewById(R.id.share_to_web);
        baby_header_icon_col_view = (BabyHeaderIconCollectionView) view.findViewById(R.id.baby_header_icon_col_view);
        View.OnClickListener addBaby = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupwindow != null && popupwindow.isShowing()) {
                    popupwindow.dismiss();
                    popupwindow = null;
                } else {
                    popupwindow = PtAddChildPopupWindow.Create(mActivity);
                    popupwindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
                    popupwindow.setOutsideTouchable(true);
                    popupwindow.setFocusable(true);
                }

            }
        };
        View.OnClickListener selectBabyListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RequestSideMoreInfo.SideMoreInfo.BabyInfo info = (RequestSideMoreInfo.SideMoreInfo.BabyInfo) v.getTag();
                CommonUtils.saveBabyId(info.id);
                baby_header_icon_col_view.show();
                selectChildSideInfo();
                Bundle bundle = new Bundle();
                BasePostEvent postEvent = new BasePostEvent(PuTaoConstants.EVENTBUS_SELECT_CHILD_SUCCESS, bundle);

                EventBus.getDefault().post(postEvent);
            }
        };
        baby_header_icon_col_view.setAddChildClickListener(addBaby);
        baby_header_icon_col_view.setSelectChildClickListener(selectBabyListener);
        body_ll = (LinearLayout) view.findViewById(R.id.body_ll);
        home_page_left_menu_collection_ll = (LinearLayout) view.findViewById(R.id.home_page_left_menu_collection_ll);
        home_page_left_menu_tag_ll = (LinearLayout) view.findViewById(R.id.home_page_left_menu_tag_ll);
        left_menu_setting_tv = (TextView) view.findViewById(R.id.left_menu_setting_tv);
        left_menu_news_tv = (TextView) view.findViewById(R.id.left_menu_news_tv);
        home_page_left_menu_tag_num_tv = (TextView) view.findViewById(R.id.home_page_left_menu_tag_num_tv);
        home_page_left_menu_collection_num_tv = (TextView) view.findViewById(R.id.home_page_left_menu_collection_num_tv);
        addEvents();
    }

    void addEvents() {
        left_menu_setting_tv.setOnClickListener(this);
        left_menu_news_tv.setOnClickListener(this);
        home_page_left_menu_collection_ll.setOnClickListener(this);
        home_page_left_menu_tag_ll.setOnClickListener(this);
        share_to_web.setOnClickListener(this);
    }

    @Override
    public void doInitDataes() {
        mShareTools = new ShareTools(getActivity());
        querySideInfo();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_menu_setting_tv:
                ActivityHelper.startActivity(mActivity, ActivitySettings.class);
                break;
            case R.id.left_menu_news_tv:
                ActivityHelper.startActivity(mActivity, ActivityMessage.class);
                break;
            case R.id.home_page_left_menu_collection_ll:
                ActivityHelper.startActivity(mActivity, ActivityTagAlbum.class);
                break;
            case R.id.home_page_left_menu_tag_ll:
                ActivityHelper.startActivity(mActivity, ActivityCollection.class);
                break;
            case R.id.share_to_web:
                showShareDialog();
                break;
        }
    }

    void showShareDialog() {
        Dialog dialog = new DialogBuilderFactory<ShareAppDialogBuilder>(getActivity(), DialogBuilderFactory.DialogType.SHAREAPP).Builder().setOnShareAppChannelListener(new ShareAppDialogBuilder.onShareAppChannelListener() {
            @Override
            public void onShare(Dialog dialog, ShareAppDialogBuilder.ShareAppChannel channel) {
                if (channel != null) {
                    String shareText = CommonUtils.getBabyName() + "的成长照片都记录在这里，赶紧戳进来看看吧，" + PuTaoConstants.SHARE_WEB_APP_URL + CommonUtils.getBabyId();
                    switch (channel) {
                        case QQ:
                            mShareTools.doInviteByQQ(shareText);
                            break;
                        case SMS:
                            ActivityHelper.startSMSActivity(mActivity, shareText);
                            break;
                        case CHAT:
                            mShareTools.doInviteByWeiXin(shareText);
                            break;
                    }
                }
                dialog.dismiss();
            }
        }).Create();
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }

    public void selectChildSideInfo() {
        if (mRequestSideMoreInfo == null) {
            return;
        }
        RequestSideMoreInfo.SideMoreInfo info = mRequestSideMoreInfo.data;
        RequestSideMoreInfo.SideMoreInfo.BabyInfo curInfo = null;
        for (int i = 0; i < info.babyList.size(); i++) {
            RequestSideMoreInfo.SideMoreInfo.BabyInfo bInfo = info.babyList.get(i);
            if (bInfo.id.equals(CommonUtils.getBabyId())) {
                curInfo = bInfo;
            }
        }
        home_page_left_menu_tag_num_tv.setText(String.valueOf(curInfo.tagAlbumNums));
        home_page_left_menu_collection_num_tv.setText(String.valueOf(curInfo.choicenessAlbumNums));

    }

    public void querySideInfo() {
        HashMap<String, String> params = new HashMap<String, String>();
        //        params.put("baby_id", CommonUtils.getBabyId());
        params.put("uid", CommonUtils.getUsrId());
        HttpRequestUtil.doHttpGetRequest(mActivity, PuTaoConstants.API_GET_SIDE_MORE_INFO,
                new RequestParams(HttpRequestUtil.buildHttpRequestParams(params)), new HttpRequestUtil.RequestResponseListener() {
                    @Override
                    public void success(int statuscode, String response) {
                        Gson gson = new Gson();
                        mRequestSideMoreInfo = gson.fromJson(response, RequestSideMoreInfo.class);
                        RequestSideMoreInfo.SideMoreInfo info = mRequestSideMoreInfo.data;
                        if (info == null) {
                            return;
                        }
                        RequestSideMoreInfo.SideMoreInfo.BabyInfo curInfo = null;
                        baby_header_icon_col_view.removeAllBabyHeaderInfo();
                        for (int i = 0; i < info.babyList.size(); i++) {
                            RequestSideMoreInfo.SideMoreInfo.BabyInfo bInfo = info.babyList.get(i);
                            if (bInfo.id.equals(CommonUtils.getBabyId())) {
                                curInfo = bInfo;
                            }
                            baby_header_icon_col_view.addBabyHeaderInfo(bInfo);
                        }
                        if (curInfo == null && info.babyList.size() > 0) {
                            curInfo = info.babyList.get(0);
                            CommonUtils.saveBabyId(curInfo.id);
                            Bundle bundle = new Bundle();
                            EventBus.getDefault().post(new BasePostEvent(PuTaoConstants.EVENTBUS_ADD_CHILD_SUCCESS, bundle));
                        }
                        if (curInfo == null) {
                            home_page_left_menu_tag_num_tv.setText(String.valueOf(0));
                            home_page_left_menu_collection_num_tv.setText(String.valueOf(0));
                        } else {
                            home_page_left_menu_tag_num_tv.setText(String.valueOf(curInfo.tagAlbumNums));
                            home_page_left_menu_collection_num_tv.setText(String.valueOf(curInfo.choicenessAlbumNums));
                        }
                        baby_header_icon_col_view.show();
                    }

                    @Override
                    public void failed(int statuscode, String response) {
                    }
                });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEvent(BasePostEvent event) {
        switch (event.eventCode) {
            case PuTaoConstants.EVENTBUS_ADD_CHILD_SUCCESS:
            case PuTaoConstants.EVENTBUS_RELATE_BABY_SUCCESS:
            case PuTaoConstants.EVENTBUS_ADD_TAG_SUCCESS:
            case PuTaoConstants.EVENTBUS_MODIFY_CHILD_SUCCESS:
                querySideInfo();
                break;
        }
    }

    @Override
    public String getRelateionShip() {
        return inviteRelationShip;
    }
}
